package com.four.peoplemanage.leave.dao;

import com.four.peoplemanage.leave.entity.Leave;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface LeaveDao {
    //申请请假
    @Insert("insert into `leave` (`information_id`,`name`,`marry`,`holidaybegintime`,`holidayendtime`,`leave_type`,`place`,`content`,`remain_holidaynumber`,`leave_status`) " +
            "values (#{information_id},#{name},#{marry},#{holidaybegintime},#{holidayendtime},#{leave_type},#{place},#{content},#{remain_holidaynumber},'待审核')")
    public int addLeave(Leave leave);

    //分页查询
    @Select("<script>" +
            "SELECT * FROM `leave`" +
            "  <where>" +
            "    <if test='name != null'>" +
            "       AND name = #{name} " +
            "    </if>" +
            "    <if test='leave_type != null'>" +
            "       AND leave_type = #{leave_type} " +
            "    </if>" +
            "    <if test='leave_status != null'>" +
            "       AND leave_status = #{leave_status} " +
            "    </if>" +
            "       and information_id = #{information_id}" +
            "  </where>" +
            "  ORDER BY CASE leave_status WHEN '待审核' THEN 1 ELSE 2 END, holidaybegintime ASC " +
            "LIMIT #{start}, #{size}" +
            "</script>")
    public List<Leave> getLeave(int start, int size, String name, String leave_type,int information_id,
                                String leave_status);
    //分页查询
    @Select("<script>" +
            "SELECT * FROM `leave`" +
            "  <where>" +
            "    <if test='name != null'>" +
            "       AND name = #{name} " +
            "    </if>" +
            "    <if test='leave_type != null'>" +
            "       AND leave_type = #{leave_type} " +
            "    </if>" +
            "    <if test='leave_status != null'>" +
            "       AND leave_status = #{leave_status} " +
            "    </if>" +
            "  </where>" +
            "  ORDER BY CASE leave_status WHEN '待审核' THEN 1 ELSE 2 END, holidaybegintime ASC " +
            "LIMIT #{start}, #{size}" +
            "</script>")
    public List<Leave> getAllLeave(int start, int size, String name, String leave_type,
                                String leave_status);
    @Select("<script>" +
            "select count(*) from `leave`" +
            "  <where>" +
            "    <if test='name != null'>" +
            "       AND name = #{name} " +
            "    </if>" +
            "    <if test='leave_type != null'>" +
            "       AND leave_type = #{leave_type} " +
            "    </if>" +
            "    <if test='leave_status != null'>" +
            "       AND leave_status = #{leave_status} " +
            "    </if>" +
            "</where>"+
            "</script>")
    public  int countLeave(String name, String leave_type,
                              String leave_status);

    //更新
    @Update("<script>" +
            "update  `leave` " +
            "        <set>" +
            "            <if test=\"null!= leave_status\">" +
            "                leave_status =#{leave_status}," +
            "            </if>" +
            "            <if test=\"null!= leave_type\">" +
            "                leave_type =#{leave_type}," +
            "            </if>" +
            "            <if test=\"null!= content\">" +
            "                content =#{content}," +
            "            </if>" +
            "        </set>" +
            "        where id =#{id}" +
            "</script>")
    public int updateLeaveById(Leave leave);

    @Update("<script>" +
            "update  `leave` " +
            "        <set>" +
            "            <if test=\"null!= leave_status\">" +
            "                leave_status =#{leave_status}," +
            "            </if>" +
            "        </set>" +
            "        where id =#{id}" +
            "</script>")
    public int updateLeave(Leave leave);


}
